package com.cooba.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.bean.ServerBean;
import com.cooba.utils.ACache;
import com.cooba.utils.Utils;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

public class ServerAdapter extends BaseAdapter {
    private final BitmapUtils bitmapUtils;
    private final ACache acache;
    private Context context;
    private List<ServerBean.ServerListInfo> list;

    public List<ServerBean.ServerListInfo> getList() {
        return list;
    }

    public void setList(List<ServerBean.ServerListInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addNewData(List<ServerBean.ServerListInfo> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<ServerBean.ServerListInfo> datas) {
        Log.d("wei", "" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public ServerAdapter(Context context, List<ServerBean.ServerListInfo> list) {
        this.context = context;
        this.list = list;
        bitmapUtils = new BitmapUtils(context);
        /**
         * 设置默认的图片展现、加载失败的图片展现
         */
        bitmapUtils.configDefaultLoadingImage(R.mipmap.default_icon1);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.default_icon1);
        acache = ACache.get(context);
    }

    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.server_list_item, parent, false);
            holder.title = (TextView) convertView.findViewById(R.id.tv_tittle);
            holder.address = (TextView) convertView.findViewById(R.id.tv_address);
            holder.phone = (TextView) convertView.findViewById(R.id.tv_phone);
            holder.img = (ImageView) convertView.findViewById(R.id.iv_icon);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }
//        String isRead = acache.getAsString("readed_server_list_" + list.get(position).getId());
//        if (!TextUtils.isEmpty(isRead) && isRead.equals("true")) {
//            holder.title.setTextColor(context.getResources().getColor(R.color.darkgray));
//        } else {
//            holder.title.setTextColor(context.getResources().getColor(R.color.black));
//        }
        holder.title.setText(Utils.replaceString(list.get(position).getName())+"("+list.get(position).getCity_name()+")");//标题
        holder.address.setText(list.get(position).getAddress());
        holder.phone.setText(list.get(position).getPhone());
        bitmapUtils.display(holder.img, list.get(position).getPic());


        return convertView;
    }


    class ViewHolder {
        TextView title;
        TextView address;
        TextView phone;
        ImageView img;

    }

}
